package com.magic.uschat.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.magic.uschat.bean.Friends;
import com.magic.uschat.util.PinyinComparator;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ViewGroup;

public class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder>{

	private List<Friends> datas = new ArrayList<Friends>();
	private OnRecyclerViewListener onRecyclerViewListener;
	@Override
	public int getItemCount() {
		return datas.size();
	}

	public void bindDatas(List<Friends> friends){
		datas.clear();
		if (friends!=null) {
			datas.addAll(friends);
		}
		Collections.sort(datas,new PinyinComparator());
	}
	
	
	public void setOnRecyclerViewListener(OnRecyclerViewListener listener){
		this.onRecyclerViewListener = listener;
	}
	
	@Override
	public void onBindViewHolder(ContactViewHolder holder, int position) {
		Friends f = datas.get(position);
		((BaseViewHolder)holder).bindData(f);
		int p = getPostionByLetter(f.getSortLetter().substring(0,1));
		//Log.e("\\\\",p+"---"+position);
		if (p==position) {
			holder.showAlpha(true);
		}else{
			holder.showAlpha(false);
		}
	}

	@Override
	public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new ContactViewHolder(parent.getContext(), parent, onRecyclerViewListener);
	}
	
	@Override
	public int getItemViewType(int position) {
		return 1;
	}

	public int getPostionByLetter(String letter){
		for(int i=0;i<datas.size();i++){
			String l = datas.get(i).getSortLetter().substring(0,1);
			if (letter.equals(l)) {
				return i;
			}
		}
		return -1;
	}
	
	public String getSecttionByPostion(int position){
		Friends f = datas.get(position);
		String l = f.getSortLetter().substring(0,1);
		return l;
	}
	
	public Friends getItem(int position){
		if (position<datas.size()) {
			return datas.get(position);
		}
		return null;
	}
	
}
